উদাহরণ সহ Custom Filter বাস্তবায়ন

Java Technologies - স্প্রিং সিকিউরিটি (Spring Security) - Spring Security এর জন্য Custom Filters
136

Spring Security তে Custom Filter তৈরি করা একটি শক্তিশালী পদ্ধতি যা আপনার নিরাপত্তা প্রক্রিয়াতে নির্দিষ্ট কাজ যেমন লগিং, অথেনটিকেশন চেক, অথরাইজেশন বা কাস্টম লজিক যোগ করতে সহায়ক। Spring Security-তে Custom Filter বাস্তবায়ন করতে হলে, আপনাকে OncePerRequestFilter বা Filter ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।

এখানে Custom Filter তৈরি করার একটি উদাহরণ দেয়া হয়েছে, যেখানে আমরা একটি কাস্টম অথেনটিকেশন ফিল্টার তৈরি করব যা প্রতি অনুরোধের সাথে ইউজারনেম এবং পাসওয়ার্ড যাচাই করবে।


Custom Filter তৈরি করার ধাপ:

Step 1: Maven Dependency

প্রথমে আপনার pom.xml ফাইলে Spring Security ডিপেন্ডেন্সি যোগ করুন, যদি আপনি এটি ইতোমধ্যে যোগ না করে থাকেন।

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Step 2: Custom Filter তৈরি করা

এখন, আমরা একটি Custom Filter তৈরি করব যা প্রতিটি HTTP অনুরোধের জন্য ইউজারনেম এবং পাসওয়ার্ড যাচাই করবে।

package com.example.security;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAuthenticationFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        // Extract the username and password from request headers or parameters
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username != null && password != null) {
            // Custom Authentication logic
            if (authenticateUser(username, password)) {
                // If authentication is successful, set the user authentication in the SecurityContext
                SecurityContextHolder.getContext().setAuthentication(
                        new UsernamePasswordAuthenticationToken(username, password));
            } else {
                // If authentication fails, return unauthorized response
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                return;
            }
        }
        
        // Continue with the filter chain
        filterChain.doFilter(request, response);
    }

    // Custom user authentication method
    private boolean authenticateUser(String username, String password) {
        // Simple hardcoded authentication logic (you can replace this with actual authentication)
        return "user".equals(username) && "password123".equals(password);
    }
}

Explanation:

  • OncePerRequestFilter: Spring Framework থেকে একটি বেস ক্লাস যা প্রতি রিকোয়েস্টে একবার ফিল্টার চালানোর গ্যারান্টি দেয়।
  • doFilterInternal(): এই মেথডে আপনি কাস্টম লজিক ব্যবহার করতে পারেন যা প্রতিটি HTTP অনুরোধের জন্য কার্যকর হবে।
  • authenticateUser(): এখানে আপনি আপনার কাস্টম অথেনটিকেশন লজিক দিতে পারেন। এই উদাহরণে আমরা সোজাসুজি username এবং password যাচাই করেছি, তবে এখানে ডাটাবেস বা অন্য সিস্টেমের সাথে ইন্টিগ্রেশন করা সম্ভব।
  • SecurityContextHolder.getContext().setAuthentication(): যদি ব্যবহারকারী সফলভাবে অথেনটিকেট হয়, তবে এটি SecurityContext এ ব্যবহারকারীর তথ্য সেট করে, যাতে পরবর্তী রিকোয়েস্টে Spring Security এর নিরাপত্তা ব্যবস্থাপনা করতে পারে।

Step 3: Custom Filter কনফিগারেশন

এখন আমাদের কাস্টম ফিল্টারকে Spring Security কনফিগারেশন ক্লাসে রেজিস্টার করতে হবে।

package com.example.config;

import com.example.security.CustomAuthenticationFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .addFilterBefore(customAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)  // Add custom filter
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()  // Public endpoints
                .anyRequest().authenticated()  // All other requests require authentication
            .and()
            .formLogin();  // Enable form login

        return http.build();
    }

    @Bean
    public CustomAuthenticationFilter customAuthenticationFilter() {
        return new CustomAuthenticationFilter();
    }
}

Explanation:

  • addFilterBefore(): এটি Spring Security এর UsernamePasswordAuthenticationFilter এর আগে আমাদের কাস্টম ফিল্টারটি চালানোর জন্য ব্যবহৃত হয়। এর মানে হল যে, আমাদের কাস্টম ফিল্টারটি UsernamePasswordAuthenticationFilter এর আগে চলে আসবে এবং এটি সব রিকোয়েস্টের জন্য কার্যকর হবে।
  • antMatchers("/public/**").permitAll(): /public/** এর মতো পাবলিক রিসোর্সগুলো কাউকে অথেনটিকেট করতে হবে না।
  • .anyRequest().authenticated(): অন্য সব রিকোয়েস্টের জন্য ব্যবহারকারীকে অথেনটিকেট করা প্রয়োজন।

Step 4: Testing Custom Filter

এখন, আপনার কাস্টম ফিল্টারটি সঠিকভাবে কাজ করছে কিনা তা পরীক্ষা করতে, আপনি একটি HTTP রিকোয়েস্ট পাঠাতে পারেন:

Example Request:

POST /login HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded

username=user&password=password123
  • যদি ইউজারনেম এবং পাসওয়ার্ড সঠিক হয়, তবে রিকোয়েস্টটি সফলভাবে প্রসেস হবে এবং SecurityContext এ ব্যবহারকারীর তথ্য সেট হবে।
  • যদি ইউজারনেম বা পাসওয়ার্ড ভুল হয়, তবে 401 Unauthorized রেসপন্স ফেরত দেওয়া হবে।

Conclusion

Spring Security তে Custom Filter বাস্তবায়ন করার মাধ্যমে আপনি আপনার নিরাপত্তা ব্যবস্থাপনা আরও কাস্টমাইজ করতে পারেন। এটি বিশেষত তখন কার্যকরী যখন আপনি নির্দিষ্ট লজিক বা ফিচার যোগ করতে চান যা Spring Security-এর ডিফল্ট ফিল্টারস দ্বারা সরাসরি সমর্থিত নয়, যেমন কাস্টম অথেনটিকেশন বা অথরাইজেশন চেক। Custom Filters সহজেই Spring Security কনফিগারেশনে যোগ করা যায় এবং আপনার অ্যাপ্লিকেশনের নিরাপত্তা চাহিদা অনুযায়ী কাস্টমাইজ করা যায়।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...